/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-9-18 下午3:07 Created by dwzhang
 */
package com.jade.bss.weixin.api.pay;

import javax.inject.Named;

import com.jade.bss.weixin.WeixinApp;
import com.jade.bss.weixin.WeixinException;
import com.jade.bss.weixin.api.pay.protocol.*;
import com.jade.bss.weixin.api.protocol.xml.XmlApi;

/**
 * 支付的API接口。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 15-9-18 下午3:07
 */
@Named ("bss_weixinPayApi")
public class PayApi
        extends XmlApi
{
    private static final String URL_UNIFIED_ORDER = "https://api.mch.weixin.qq.com/pay/unifiedorder";
    private static final String URL_ORDER_QUERY = "https://api.mch.weixin.qq.com/pay/orderquery";
    private static final String URL_CLOSE_ORDER = "https://api.mch.weixin.qq.com/pay/closeorder";
    private static final String URL_REFUND = "https://api.mch.weixin.qq.com/secapi/pay/refund";
    private static final String URL_REFUND_QUERY = "https://api.mch.weixin.qq.com/pay/refundquery";
    private PayListener listener;

    public void setListener(PayListener listener)
    {
        this.listener = listener;
    }

    public UnifiedOrderResponse unifiedOrder(String code, UnifiedOrderRequest request)
            throws WeixinException
    {
        WeixinApp app = getApp(code);
        return unifiedOrder(app, request);
    }

    public UnifiedOrderResponse unifiedOrder(WeixinApp app, UnifiedOrderRequest request)
            throws WeixinException
    {
        setCommonParam(app, request);
        return submitRequest(URL_UNIFIED_ORDER, app, request, true, UnifiedOrderResponse.class);
    }

    public OrderQueryResponse orderQuery(String code, OrderQueryRequest request)
            throws WeixinException
    {
        WeixinApp app = getApp(code);
        return orderQuery(app, request);
    }

    public OrderQueryResponse orderQuery(WeixinApp app, OrderQueryRequest request)
            throws WeixinException
    {
        setCommonParam(app, request);
        return submitRequest(URL_ORDER_QUERY, app, request, true, OrderQueryResponse.class);
    }

    public CloseOrderResponse closeOrder(String code, CloseOrderRequest request)
            throws WeixinException
    {
        WeixinApp app = getApp(code);
        return closeOrder(app, request);
    }

    public CloseOrderResponse closeOrder(WeixinApp app, CloseOrderRequest request)
            throws WeixinException
    {
        setCommonParam(app, request);
        return submitRequest(URL_CLOSE_ORDER, app, request, true, CloseOrderRequest.class);
    }

    public RefundResponse refund(String code, RefundRequest request)
            throws WeixinException
    {
        WeixinApp app = getApp(code);
        return refund(app, request);
    }

    public RefundResponse refund(WeixinApp app, RefundRequest request)
            throws WeixinException
    {
        setCommonParam(app, request);
        return submitRequest(URL_REFUND, app, request, true, RefundResponse.class);
    }

    public RefundQueryResponse refundQuery(String code, RefundQueryRequest request)
            throws WeixinException
    {
        WeixinApp app = getApp(code);
        return refundQuery(app, request);
    }

    public RefundQueryResponse refundQuery(WeixinApp app, RefundQueryRequest request)
            throws WeixinException
    {
        setCommonParam(app, request);
        return submitRequest(URL_REFUND_QUERY, app, request, true, RefundQueryRequest.class);
    }

    public NotifyResponse onNotify(String code, NotifyRequest request)
    {
        if (listener != null) {
            return listener.onNotify(getApp(code), request);
        }
        else {
            return new NotifyResponse();
        }
    }
}
